﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;


namespace AgileShare.Framework.SiteProvisioning.Definitions
{
	/// <summary>
	/// The configuration of a field that can be used to add a new field to SharePoint. The DefinitionXml property returns the field in a valid SharePoint XML format.
	/// </summary>
	/// <example>	
	/// FieldDefinition fieldDefinition = new FieldDefinition(new Guid("{F56D2B4F-242A-4169-89F9-F3CA0AB33E8A}"), "MyFieldName", "My Field Display Name", "My Field Description", true, "My Group");
	/// 
	/// SPFieldHelper.AddField(fieldDefinition, list.Fields); // Add to SPList fields
	/// SPFieldHelper.AddField(fieldDefinition, web.Fields); // Add to SPWeb fields
	/// </example>
	public class FieldDefinitionDateTime : FieldDefinition
	{
		/// <summary>
		/// Gets or sets the format.
		/// </summary>
		/// <value>
		/// The format.
		/// </value>
		public FieldFormatDateTime? Format { get; set; }

		public FieldDefinitionDateTime()
		{
			base.Type = FieldType.DateTime;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="FieldDefinitionDateTime"/> class.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <param name="name">The name.</param>
		/// <param name="displayName">The display name.</param>
		/// <param name="description">The description.</param>
		/// <param name="required">if set to <c>true</c> [required].</param>
		/// <param name="group">The group.</param>
		/// <param name="format">The format.</param>
		public FieldDefinitionDateTime(Guid id, string name, string displayName, string description, bool required, string group, FieldFormatDateTime? format, params object[] additionalData) :
			base(id, FieldType.DateTime, name, displayName, description, required, group, additionalData)
		{
			this.Format = format;
		}

		new public FieldType Type
		{
			get
			{
				return FieldType.DateTime;
			}
		}

		/// <summary>
		/// Gets the definition XML.
		/// </summary>
		public override System.Xml.Linq.XElement DefinitionXml
		{
			get
			{
				var field = base.DefinitionXml;

				AddAttributeIfNotNull(field, "Format", this.Format);

				return field;
			}
		}
	}
}
